3 * Object caching using WinCache.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Wrapper for WinCache object caching functions; identical interface
30 class WinCacheBagOStuff
extends BagOStuff
{
31 protected function doGet( $key, $flags = 0, &$casToken = null ) {
34 $blob = wincache_ucache_get( $key );
35 if ( !is_string( $blob ) ) {
39 $value = unserialize( $blob );
40 if ( $value !== false ) {
41 $casToken = (string)$blob; // don't bother hashing this
47 protected function cas( $casToken, $key, $value, $exptime = 0, $flags = 0 ) {
48 if ( !wincache_lock( $key ) ) { // optimize with FIFO lock
52 $curCasToken = null; // passed by reference
53 $this->doGet( $key, self
::READ_LATEST
, $curCasToken );
54 if ( $casToken === $curCasToken ) {
55 $success = $this->set( $key, $value, $exptime, $flags );
58 __METHOD__
. ' failed due to race condition for {key}.',
62 $success = false; // mismatched or failed
65 wincache_unlock( $key );
70 public function set( $key, $value, $expire = 0, $flags = 0 ) {
71 $result = wincache_ucache_set( $key, serialize( $value ), $expire );
73 return ( $result === [] ||
$result === true );
76 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
77 $result = wincache_ucache_add( $key, serialize( $value ), $exptime );
79 return ( $result === [] ||
$result === true );
82 public function delete( $key, $flags = 0 ) {
83 wincache_ucache_delete( $key );
89 * Construct a cache key.
92 * @param string $keyspace
96 public function makeKeyInternal( $keyspace, $args ) {
97 // WinCache keys have a maximum length of 150 characters. From that,
98 // subtract the number of characters we need for the keyspace and for
99 // the separator character needed for each argument. To handle some
100 // custom prefixes used by thing like WANObjectCache, limit to 125.
101 // NOTE: Same as in memcached, except the max key length there is 255.
102 $charsLeft = 125 - strlen( $keyspace ) - count( $args );
105 function ( $arg ) use ( &$charsLeft ) {
106 // 33 = 32 characters for the MD5 + 1 for the '#' prefix.
107 if ( $charsLeft > 33 && strlen( $arg ) > $charsLeft ) {
108 $arg = '#' . md5( $arg );
111 $charsLeft -= strlen( $arg );
117 if ( $charsLeft < 0 ) {
118 return $keyspace . ':BagOStuff-long-key:##' . md5( implode( ':', $args ) );
121 return $keyspace . ':' . implode( ':', $args );
125 * Increase stored value of $key by $value while preserving its original TTL
126 * @param string $key Key to increase
127 * @param int $value Value to add to $key (Default 1)
128 * @return int|bool New value or false on failure
130 public function incr( $key, $value = 1 ) {
131 if ( !wincache_lock( $key ) ) { // optimize with FIFO lock
135 $n = $this->doGet( $key );
136 if ( $this->isInteger( $n ) ) {
137 $n = max( $n +
(int)$value, 0 );
138 $oldTTL = wincache_ucache_info( false, $key )["ucache_entries"][1]["ttl_seconds"];
139 $this->set( $key, $n, $oldTTL );
144 wincache_unlock( $key );